﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Web;
using Antu.WorkFlowFactory;
using System.Data;
using System.Configuration;


namespace WebSys.UI.OfficeFlow
{
    public class SuspendListSkin : MPBasicTemplate
    {
        FlowFactoryProvider _FlowFactoryProvider = (FlowFactoryProvider)ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");
        DropDownList ddlWname;
        Button btnOK;
        CustomGridView GV;
        Label labMsg;
        TextBox txtIId, txtName;
        protected string UserId
        {
            get { if (base.Page.Session["UserId"] != null) { return base.Page.Session["UserId"].ToString(); } else { return string.Empty; } }
            set { base.Page.Session["UserId"] = value; }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            InitControl();
            labMsg.Text = "";
            if (!base.Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(base.Page.Request["action"]))
                {
                    string action = base.Page.Request["action"];
                    string iid = base.Page.Request["iid"].ToString();
                    string uid = base.Page.Session["UserId"].ToString();
                    string res = _FlowFactoryProvider.NewDelApply(iid, uid);
                    if (!string.IsNullOrEmpty(res))
                    {
                        base.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('解挂失败！')</script>");
                    }
                    else
                    {
                        base.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('解挂成功！')</script>");
                    }
                }
                BindDDL();
                BindGV();
            }
            GV.OnLoadData += new EventHandler(GV_OnLoadData);
        }

        protected void GV_OnLoadData(object sender, EventArgs e)
        {
            BindGV();
        }

        private void InitControl()
        {
            ddlWname = (DropDownList)FindControl("ddlWname");
            btnOK = (Button)FindControl("btnOK");
            btnOK.Click += new EventHandler(btnOK_Click);
            GV = (CustomGridView)FindControl("GV");
            GV.RowCreated += new GridViewRowEventHandler(GV_OnRowCreated);
            labMsg = (Label)FindControl("labMsg");
            txtIId = (TextBox)FindControl("txtIId");
            txtName = (TextBox)FindControl("txtName");
        }

        protected void BindGV()
        {
            try
            {
                string type = base.Page.Request["type"];
                if (string.IsNullOrEmpty(type)) { type = "yjg"; }
                DataTable dt = _FlowFactoryProvider.NewGetSuspendListByUser(txtIId.Text.Trim(), txtName.Text.Trim(), ddlWname.SelectedValue, "", type);
                GV.DataSource = dt;
                GV.RecordCount = dt.Rows.Count;
                GV.DataBind();
                labMsg.Text = dt.Rows.Count == 0 ? "无匹配数据" : "";
            }
            catch (Exception)
            {
                labMsg.Text = "获取数据出错";
            }
        }
        private void BindDDL()
        {
            try
            {
                string ajlx = ConfigurationManager.AppSettings["ZSAnJianType"].ToString(); 
                DataTable dt = _FlowFactoryProvider.GetSuspendListWname(UserId,ajlx);
                ddlWname.DataSource = dt;
                ddlWname.DataTextField = "wname";
                ddlWname.DataValueField = "wname";
                ddlWname.DataBind();

                ListItem item = new ListItem();
                item.Text = dt.Rows.Count == 0 ? "-- 无确定挂起流程 --" : "-- 所有流程 --";
                item.Value = "";
                ddlWname.Items.Insert(0, item);
            }
            catch (Exception)
            {
                labMsg.Text = "绑定选项出错";
            }

        }
        protected void btnOK_Click(object sender, EventArgs e)
        {
            BindGV();
        }

        protected void GV_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView dtr = (DataRowView)e.Row.DataItem;
                string id = dtr["ID"].ToString();
                string iid = dtr["IID"].ToString();
                HyperLink hldel = e.Row.FindControl("hydel") as HyperLink;
                TableCell cell = new TableCell();
                int temp = Convert.ToInt32(_FlowFactoryProvider.GetSomeStr("st_suspend", "count(*)", " and end_date is null and id='" + id + "'"));
                if (temp > 0)
                {
                    hldel.Text = "解挂";
                    hldel.Attributes.Add("onclick", "return confirm('您确定解挂？')");
                    string strurl = "../../../../ZhengShou/OfficeFlow/SuspendList.aspx?type=wjg&action=del&iid=" + iid;
                    hldel.NavigateUrl = strurl;
                }
                else
                {
                    hldel.Text = "已解挂";
                    hldel.Attributes.Add("onclick", "");
                    hldel.NavigateUrl = "#";
                    hldel.Enabled = false;
                }
            }

        }

    }
}


